<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function showlist(){
        header('Content-type:text/html;charset=utf-8');
        //echo \Tools\Page::$name;
        $goods = D("goods");
        $cnt = $goods->count();//总条数
        $per = 7;//每页显示的条数
        
        //2实例化分页类对象
        
        $page_obj = new \Tools\Page($cnt,$per);
        
        $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit; 
        
        $info = $goods->query($sql);
        
        $pagelist = $page_obj->fpage(array(3,4,5,6,7,8));
        
        $this->assign('pagelist',$pagelist);
        
        //$reslut = $goods->select(); 
        //$goods = new \Model\GoodsModel();
        //dump($reslut);
        //$reslut = $goods->select("2,10,11,12,66");
        //$reslut = $goods -> where("goods_price>1000 and goods_name like '诺%'")->select();
        //$reslut = $goods ->limit(10)->select();
        //排序查询
        //$reslut = $goods ->order("goods_price")->select();
        //$reslut = $goods ->field('goods_brand_id,goods_name')->select();
        //$reslut = $goods ->group('goods_brand_id')->select();
        $this->assign("info",$info);
        $this->display();
    }
    public function upd($goods_id){
        header('Content-type:text/html;charset=utf-8');
        $goods = D('goods');
        if(!empty($_POST)){
           $z = $goods->save($_POST);
           //dump($_POST);
           if($z){
               $this->redirect('showlist',array('title'=>'tianjiacg'),2,'修改成功');
           } else{
               $this->redirect('upd',array('goods_id'=>$goods_id),2,'修改失败');
           }
        } else {
            $z = $goods->find($goods_id);
            $this ->assign('info',$z);
            //dump($goods_id);
            $this->display();
        }
        
       
    }
    public function add(){
        header('Content-type:text/html;charset=utf-8');
        $goods =D('goods');
        if(!empty($_POST)){
            if($_FILES['goods_image']['error']===0){
                $cfg = array(
                    'rootPath' => './Public/Upload/',//文件保存根目录  
                );
                $up = new \Think\Upload($cfg);
                
                $z = $up->uploadOne($_FILES['goods_image']);
                $bigpicname = $up->rootPath.$z['savepath'].$z['savename'];
                $_POST['goods_big_img'] = substr($bigpicname, 2);
                
                $im = new \Think\Image();
                $im->open($bigpicname)->thumb(125,125);
                $smallpciname = $up->rootPath.$z['savepath']."small_".$z['savename'];
                $im->save($smallpciname);
                $_POST['goods_small_img'] = substr($smallpciname, 2);
            }
            $res= $goods->add($_POST);
            if($res){
                $this->redirect('showlist',array(),2,'添加成功');  
            }  else {
                $this->redirect('add',array(),2,'添加失败');
            }
        } else{
            $this->display();
        };
//         $arr = array(
//             'goods_name'=>'黑莓手机',
//             'goods_price' =>1300,
//             'goods_num' =>14,
//             'goods_weight' =>255
//         );
//         $goods ->goods_name='黑莓手机';
//         $goods ->goods_price=1300;
//         $goods ->goods_num=14;
//         $goods ->goods_weight=255;
//        $res=$goods->add();
//        echo $res;
        
    }

}